package org.rcy.ruicingmarket.controller;

import org.rcy.ruicingmarket.comment.CommentAddVo;
import org.rcy.ruicingmarket.comment.CommentEditVo;
import org.rcy.ruicingmarket.comment.CommentQueryRequest;
import org.rcy.ruicingmarket.comment.CommentService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author RuicyWu
 * @Date 2022/3/25 10:23
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    protected final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private CommentService commentService;

    @PostMapping("/add")
    public ResultMessage addComment(@RequestBody CommentAddVo vo){
     try {
         String uid = vo.getUid();
         if(StringUtils.isBlank(uid)){
             return ResultMessage.error();
         }
         vo.setUid(uid);
         commentService.add(vo);
         return ResultMessage.success();
     }catch (Exception e){
        logger.error(e.getMessage(),e);
         return ResultMessage.error();
     }
    }

    @DeleteMapping("/del/{id}")
    public ResultMessage delComment(@PathVariable("id")String id){
        try {
            commentService.removeById(id);
            return  ResultMessage.success();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }


    @PutMapping("/edit")
    public ResultMessage editComment(@RequestBody CommentEditVo vo){
        try {
            commentService.update(vo);
            return ResultMessage.success();
        }catch (Exception e){

            return ResultMessage.error();
        }
    }

    @PostMapping("/commentlist")
    public ResultMessage getCommentList(@RequestBody CommentQueryRequest request){
        try {
            return ResultMessage.success(commentService.getCommentList(request));
        }catch (Exception e){
            logger.error(e.getMessage(),e);
         return ResultMessage.error();
        }
    }

    @PostMapping("/replies")
    public ResultMessage getReplies(@RequestBody CommentQueryRequest request){
         try {
             return ResultMessage.success(commentService.findByParentId(request));
         }catch (Exception e){
             logger.error(e.getMessage(),e);
             return ResultMessage.error();
         }
    }

    /**
     * 点赞
     * @param id
     * @return
     */
    @GetMapping("/thumbs/{id}")
    public ResultMessage thumbsUpComment(@PathVariable("id")String id, HttpServletRequest request){
        try {
            String uid = (String)request.getHeader("uid");
            if(StringUtils.isEmpty(uid)){
                System.out.println(uid);
                logger.error("token异常");
                return ResultMessage.error();
            }
            commentService.commentThumbup(id,uid);
            return ResultMessage.success();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    /**
     * 取消点赞
     * @param id
     * @return
     */
    @GetMapping("/cancelthumbs/{id}/{uid}")
    public ResultMessage cancelThumbsUp(@PathVariable("id")String id, HttpServletRequest request){
        try {
            String uid = (String)request.getHeader("uid");
            if(StringUtils.isEmpty(uid)){
                return ResultMessage.error();
            }
            commentService.cancelCommentThumbup(id,uid);
            return ResultMessage.success();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }
}
